package cn.edu.nju.ws.sview.wyge.relations.datasets;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.edu.nju.ws.sview.database.DBConnectionFactory;

public class DatasetName {
	private String name;
	private String lang;
	private int id = -1;
	private DatasetItem ds;
	
	public DatasetName(DatasetItem ds, String name, String lang){
		this.ds = ds;
		this.name = name;
		this.lang = lang;
	}
	public String toString() {
		return id+":"+name+"@"+lang;
	}
	public boolean equals(Object o){
		if (o == this)
			return true;
		if (!(o instanceof DatasetName))
			return false;
		DatasetName that = (DatasetName) o;
		if (that.toString().equals(this.toString())) {
			return true;
		}
		return false;
	}
	public String getText(){
		return this.name;
	}
	public String getLang(){
		return this.lang;
	}
	public int getId(){
		if(id<0 && ds.getGenre().contentEquals(DatasetItem.CUSTOM_GENRE)){
			try {
				Connection conn = DBConnectionFactory.getConnection();
				PreparedStatement pstmt = conn.prepareStatement("select id from custom_dataset_label where ds_id=? and name=? and lang=?");
				pstmt.setInt(1, ds.getId());
				pstmt.setString(2, name);
				pstmt.setString(3, lang);
				ResultSet rs = pstmt.executeQuery();
				if(rs.next()){
					id = rs.getInt(1);
				}
				rs.close();
				pstmt.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return id;
	}
}
